//
//  ResultatsListViewController.m
//  FinApp
//
//  Created by Marcel Arbó Lack on 12/11/11.
//  Copyright (c) 2011 FIB-Fàcultat d'informàtica de Barcelona. All rights reserved.
//

#import "ResultatsListViewController.h"
#import "ResultadosBusquedaViewController.h"
#import "ListFlat.h"
#import "FlatListNearCell.h"

@implementation ResultatsListViewController

@synthesize flatsTable = _flatsTable;
@synthesize parentVC = _parentVC;
@synthesize filtrosView = _filtrosView;
@synthesize filtrarLabel, precioLabel, superficieLabel, habitacionsLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //Set localized title for labels
    [filtrarLabel setText:NSLocalizedString(@"_filtrar_resultados_por", @"")];
    [precioLabel setText:NSLocalizedString(@"_precio", @"")];
    [superficieLabel setText:NSLocalizedString(@"_superficie", @"")];
    [habitacionsLabel setText:NSLocalizedString(@"_habitaciones", @"")];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    [self setFiltrarLabel:nil];
    [self setPrecioLabel:nil];
    [self setSuperficieLabel:nil];
    [self setHabitacionsLabel:nil];
    [_filtrosView release];
    [_flatsTable release];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void) dealloc {
    [self setFiltrarLabel:nil];
    [self setPrecioLabel:nil];
    [self setSuperficieLabel:nil];
    [self setHabitacionsLabel:nil];
    [_filtrosView release];
    [_flatsTable release];
    [super dealloc];
}

#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
	return 1;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 96;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
	return [[_parentVC flats] count];
}


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row%2 == 0)
		[cell setBackgroundColor:[UIColor colorWithRed:62/255.0 green:155/255.0 blue:130/255.0 alpha:0.25]];
	else
		[cell setBackgroundColor:[UIColor colorWithRed:241/255.0 green:241/255.0 blue:241/255.0 alpha:1]];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *CellIdentifier = @"FlatListNearCell";
    FlatListNearCell *cell = (FlatListNearCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
	if (cell == nil) { 
		UIViewController *customCell = [[UIViewController alloc] initWithNibName:@"FlatListNearCell" bundle:nil];
        cell = (FlatListNearCell*)customCell.view;
        [customCell release];
    }
    
	[cell setCellComponentsWithFlat:[[_parentVC flats] objectAtIndex:indexPath.row]];
    [cell.distanceLabel setHidden:YES];
    
	return cell;
}

#pragma mark Table Delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	ListFlat *flat = [_parentVC.flats objectAtIndex:indexPath.row];
    [_parentVC goToDetail:flat];
    
    [_flatsTable deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - Private Methods

- (IBAction)filterButtonPressed:(id)sender {
    if (_filtrosView.tag == 0) {
        _filtrosView.tag = 1;
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.7];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [_filtrosView setFrame:CGRectMake(0, 0, 320, 396)];
        [UIView commitAnimations];
    }
    else {
        _filtrosView.tag = 0;
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.7];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [_filtrosView setFrame:CGRectMake(0, 362, 320, 396)];
        [UIView commitAnimations];
    }
}


@end
